package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.common.core.domain.Ztree;
import com.xhh.nphm.common.utils.StringUtils;
import com.xhh.nphm.system.domain.AppealCate;
import com.xhh.nphm.system.mapper.AppealCateMapper;
import com.xhh.nphm.system.service.IAppealCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppealCateServiceImpl extends ServiceImpl<AppealCateMapper,AppealCate> implements IAppealCateService {
    @Autowired
    private AppealCateMapper appealCateMapper;
    /**
     * 内容分类列表数据
     * @param appealCate
     * @return
     */
    @Override
    public List<AppealCate> contentSortJson(AppealCate appealCate) {
       return appealCateMapper.contentSortJson(appealCate);
    }

    /**
     * 根据codepk 查询内容分类
     * @param codepk
     * @return
     */
    @Override
    public AppealCate selectByCodePk(String codepk) {
        return appealCateMapper.selectByCodePk(codepk);
    }

    /**
     * 校验内容名称
     * @param appealCate
     * @return
     */
    @Override
    public String checkSortNameUnique(AppealCate appealCate) {
        String deptId = StringUtils.isNull(appealCate.getCodepk()) ? "-1" : appealCate.getCodepk();
        QueryWrapper<AppealCate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SortName",appealCate.getSortname()).eq("CodeParent",appealCate.getCodeparent());
        List<AppealCate> info = appealCateMapper.selectList(queryWrapper);
        if (info!=null&&info.size()>1) {
            return "1";
        }
        return "0";
    }

    /**
     * 查询对应 内容分类
     * @param codepk
     * @return
     */
    @Override
    public AppealCate selectSortById(String codepk) {
        QueryWrapper<AppealCate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CodePK",codepk)
                .gt("CodePK",">11")
                .eq("isDel",0)
                .eq("UseFlag",1).eq("EndFlag",0);
        return appealCateMapper.contentSortJson(new AppealCate()) ==null || appealCateMapper.contentSortJson(new AppealCate()).size()==0?null : appealCateMapper.contentSortJson(new AppealCate()).get(0);
//        return appealCateMapper.selectList(queryWrapper)==null||appealCateMapper.selectList(queryWrapper).size()==0 ?  null : appealCateMapper.selectList(queryWrapper).get(0);
    }
    /**
     * 加载内容分类列表树（排除下级）
     */
    @Override
    public List<Ztree> selectSortTreeExcludeChild(AppealCate appealCate) {
        List<AppealCate> deptList = appealCateMapper.contentSortJson(appealCate);
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
    }
    /**
     * 加载内容分类列表树
     */
    @Override
    public List<Ztree> selectSortTree(AppealCate appealCate) {
        List<AppealCate> deptList = appealCateMapper.contentSortJson(appealCate);
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
    }

    @Override
    public int selectSortCount(String codepk) {
        AppealCate appealCate = new AppealCate();
        appealCate.setCodeparent(codepk);
        QueryWrapper<AppealCate> queryWrapper = new QueryWrapper<>();
        if(appealCate.getCodepk()!=null)
            queryWrapper.eq("CodePK",appealCate.getCodepk());
        if(appealCate.getCodeparent()!=null)
            queryWrapper.eq("CodeParent",appealCate.getCodeparent());
        return appealCateMapper.selectCount(queryWrapper);
    }

    @Override
    public int deleteByCodePk(String codepk) {
        return appealCateMapper.deleteByCodePk(codepk);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<AppealCate> deptList) {
        return initZtree(deptList, null);
    }
    /**
     * 对象转部门树
     *
     * @param sortList     内容列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<AppealCate> sortList, List<String> roleDeptList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (AppealCate sort : sortList) {
                Ztree ztree = new Ztree();
                ztree.setId(Long.parseLong(sort.getCodepk()));
                ztree.setpId(Long.parseLong(sort.getCodeparent()==null ? "0" : sort.getCodeparent()));
                ztree.setName(sort.getSortname());
                ztree.setTitle(sort.getSortname());
                if (isCheck) {
                    ztree.setChecked(roleDeptList.contains(sort.getCodepk() + sort.getSortname()));
                }
                ztrees.add(ztree);
        }
        return ztrees;
    }

}
